<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" import="java.util.*,java.text.*,kk.*" pageEncoding="UTF-8"%>
<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'jieshou1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
      DiskFileItemFactory df=new DiskFileItemFactory();
      ServletFileUpload upload=new ServletFileUpload(df);
      upload.setHeaderEncoding("utf-8");
      List<FileItem> list=upload.parseRequest(request);
      //out.print("<script>alert(1)</script>");
      News n=new News();
      NewsDao ndao=new NewsDao();
      String picpath=null;
      boolean is=false;
      for(FileItem f:list){
    	  if(f.isFormField()){
    	  	out.print("这是普通的表单元素："+f.getFieldName()+"="+f.getString("utf-8"));
    	  	if(f.getFieldName().equals("ntid")){
    	  		Long ntid=Long.parseLong(f.getString("utf-8"));
    	  		n.setNtid(ntid);
    	  	}	
    	  	else if(f.getFieldName().equals("nid")){
    	  		Long nid=Long.parseLong(f.getString("utf-8"));
    	  		n.setNid(nid);
    	  		picpath=ndao.get(nid).getNpicpath();		
    	  	}
    	  	else if(f.getFieldName().equals("ntitle")){
    	  		String s=f.getString("utf-8");
    	  		n.setNtitle(s);	
    	  	}
    	  	else if(f.getFieldName().equals("nauthor")){
    	  		String s=f.getString("utf-8");
    	  		n.setNauthor(s);	
    	  	}
    	  	else if(f.getFieldName().equals("nsummary")){
    	  		String s=f.getString("utf-8");
    	  		n.setNsummary(s);	
    	  	}	
    	  	else if(f.getFieldName().equals("ncontent")){
    	  		String s=f.getString("utf-8");	
    	  		n.setNcontent(s);	
    	  	}
    	  }else{
    		  String filePath=application.getRealPath("images");
    		  out.print(filePath);
    		  System.out.print("这是一个文件元素：文件名是："+f.getName());
    		  String oldName=f.getName();
    		  //System.out.print(oldName);
    		  if(!(oldName.equals(""))){
	    		  String houzhui=oldName.substring(oldName.lastIndexOf('.'));
	    		  String newFileName=UUID.randomUUID()+houzhui;
	    		  f.write(new java.io.File(filePath,newFileName));//内容
	    		  //String pic=filePath+"\\"+newFileName;
	    		  String pic="images/"+newFileName;
	    		  out.print(pic);
	    		  n.setNpicpath(pic);
	    		  is=true;
	    		  //out.print("<img src='images/"+newFileName+"'/>");
    		  }
    	  }
    	  out.print("<hr/>");
      }
      Date date = new Date(System.currentTimeMillis());
	  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  String d=dateFormat.format(date);
	  n.setNmodifydate(d);
	  int i=0,i1=0;
	  if(request.getParameter("opr").equals("updateNews")){
		  if(is){
		  	i=ndao.update1(n);
		  	}
		  else{
		  	n.setNpicpath(picpath);
		  	i=ndao.update1(n);
		  	}
		  if(i>0)
		  	out.print("<script>alert('更新成功！')</script>");
		  else
		  	out.print("<script>alert('更新失败！')</script>");
	  }  
	  if(request.getParameter("opr").equals("addNews")){
	  	  if(is){
		  	i1=ndao.add(n);
		  	}
		  else{
		  	n.setNpicpath(picpath);
		  	i1=ndao.add(n);
		  	}
		  if(i1>0)
		  	out.print("<script>alert('增加新闻成功！')</script>");
		  else
		  	out.print("<script>alert('增加新闻失败！')</script>");
	  }	
    %> <br>
  </body>
</html>